﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2014-04-30 0:25
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BassDrop.Core.Common.Data.IO.Portable;

namespace BassDrop.Core.Common.Utilities
{
	/// <summary>
	/// Contains extension methods for IFileHandler objects
	/// </summary>
	public static class FileHandlerUtils
	{
		/// <summary>
		/// Marks a file deleted
		/// </summary>
		/// <param name="fileHandler">the file handler to use for the operation</param>
		/// <param name="filePath">path to the file to mark as deleted</param>
		public static void MarkDeleted( this IFileHandler fileHandler, string filePath )
		{
			if ( fileHandler.Exists( filePath ) )
			{
				fileHandler.Delete( filePath );
				fileHandler.WriteAllText( Registry.ToDeletedPath( filePath ), "" );
			}
		}

		/// <summary>
		/// Marks a file not deleted if it is marked deleted.
		/// This does not restore the file.
		/// </summary>
		/// <param name="fileHandler">the file handler to use for the operation</param>
		/// <param name="filePath">path to the file to mark as not deleted</param>
		public static void UnMarkDeleted( this IFileHandler fileHandler, string filePath )
		{
			string deletedPath = Registry.ToDeletedPath( filePath );
			if ( fileHandler.Exists( deletedPath ) )
			{
				fileHandler.Delete( deletedPath );
			}
		}

		/// <summary>
		/// Checks if a file is marked deleted (it existed earlier but was since deleted). 
		/// It is possible, that a file exists yet it is marked deleted, but that generally suspects the wrong use of the API.
		/// It is also possible, that a file is deleted, yet it isn't marked so if it has been deleted with Delete() instead of
		/// MarkDeleted().
		/// </summary>
		/// <param name="fileHandler">the file handler to use for the operation</param>
		/// <param name="filePath">path to the file to check</param>
		public static bool IsMarkedDeleted( this IFileHandler fileHandler, string filePath )
		{
			return fileHandler.Exists( Registry.ToDeletedPath( filePath ) );
		}

		/// <summary>
		/// Checks if a file is marked deleted (it existed earlier but was since deleted) and returns the deletion metadata if it is. 
		/// It is possible, that a file exists yet it is marked deleted, but that generally suspects the wrong use of the API.
		/// It is also possible, that a file is deleted, yet it isn't marked so if it has been deleted with Delete() instead of
		/// MarkDeleted().
		/// </summary>
		/// <param name="fileHandler">the file handler to use for the operation</param>
		/// <param name="filePath">path to the file to get data for</param>
		public static FileMetadata GetDeletionMetadata( this IFileHandler fileHandler, string filePath )
		{
			string deletedPath = Registry.ToDeletedPath( filePath );
			if ( fileHandler.Exists( deletedPath ) )
			{
				return fileHandler.GetMetadata( deletedPath );
			}

			return null;
		}
	}
}
